package com.woniuxy.shirocore;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class RealmBasicDemo {
    public static void main(String[] args) {
        //读取配置文件
        BasicIniEnvironment basicIniEnvironment = new BasicIniEnvironment("classpath:ini/shiro-realm.ini");
        //创建中央大脑
        SecurityManager securityManager = basicIniEnvironment.getSecurityManager();
        //使用工具类
        SecurityUtils.setSecurityManager(securityManager);
        //创建subject
        Subject subject = SecurityUtils.getSubject();
        //模拟前端传过来的数据，通过login方法交给securityManager校验是否正确
        subject.login(new UsernamePasswordToken("xiaozhang","123456"));
        //登陆成功
        System.out.println(subject.getPrincipal()+"登陆成功"+subject.isAuthenticated());
        /*校验权限*/
        //检查角色
        subject.checkRole("admin");
        //检查权限
        subject.checkPermission("manager:add");
    }
}
